package com.fudan.backend.controller;

import com.fudan.backend.exception.BaseException;
import com.fudan.backend.exception.RetCode;
import com.fudan.backend.pojo.CartItem;
import com.fudan.backend.service.CartService;
import com.fudan.backend.util.JwtUtil;
import com.fudan.backend.util.R;
import com.fudan.backend.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/cart")
public class CartController {
    @Autowired
    CartService cartService;
    @Autowired
    StringUtil stringUtil;


    @GetMapping("/{userId}")
    public R getUserCart(@PathVariable String userId, @RequestHeader String token) {
        if (!JwtUtil.getUserId(token).equals(userId)) {
            return new R(RetCode.UNAUTHORIZED_REQUEST.getCode(), RetCode.UNAUTHORIZED_REQUEST.getMessage());
        }
        List<List<CartItem>> cartItemGroupList = cartService.getCartItems(userId);
        return new R(RetCode.CART_SUCCESS.getCode(), RetCode.CART_SUCCESS.getMessage(), cartItemGroupList);
    }

    @PostMapping("/add")
    public R addCart(@RequestBody Map<String, Object> map) {
        String userId, commodityId, storeId;
        Integer commodityNum;
        try {
            userId = map.get("userId").toString();
            commodityId = map.get("commodityId").toString();
            storeId = map.get("storeId").toString();
            commodityNum = Integer.parseInt(map.get("commodityNum").toString());
        } catch (Exception e) {
            throw new BaseException(RetCode.INVALID_REQUEST);
        }
        RetCode retCode = cartService.addCart(userId, commodityId, storeId, commodityNum);
        return new R(retCode);
    }

    @PostMapping("/delete")
    public R deleteCartItem(@RequestBody Map<String, Object> map) {
        String cartItemId;
        try {
            cartItemId = map.get("cartItemId").toString();
        } catch (Exception e) {
            throw new BaseException(RetCode.INVALID_REQUEST);
        }
        RetCode retCode = cartService.deleteCart(cartItemId);
        return new R(retCode);
    }

    @PostMapping("/delete-selected")
    public R deleteCartItems(@RequestBody Map<String, Object> map) {
        List<String> list;
        try {
            list = stringUtil.objToList(map.get("cartItemIdList"));
        } catch (Exception e) {
            throw new BaseException(RetCode.INVALID_REQUEST);
        }
        RetCode retCode = cartService.deleteCartList(list);
        return new R(retCode);
    }

    @PostMapping("/modify")
    public R modifyCartItemCommodityNum(@RequestBody Map<String, Object> map) {
        String cartItemId;
        Integer amount;
        try {
            cartItemId = map.get("cartItemId").toString();
            amount = Integer.parseInt(map.get("commodityNum").toString());
        } catch (Exception e) {
            throw new BaseException(RetCode.INVALID_REQUEST);
        }
        RetCode retCode = cartService.modifyCommodityNum(cartItemId, amount);
        return new R(retCode);
    }
}
